﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace WorkflowTaskReplicator
{
    public class TaskItemEventReceiver : SPItemEventReceiver
    {
        /// <summary>
        /// Synchronous Before event that occurs when a new item is added to its containing object.
        /// </summary>
        /// <param name="properties"></param>
        public override void ItemAdding(SPItemEventProperties properties)
        {
            if (Helper.ListItemIsTask(properties.ListItem))
            {

            }

            base.ItemAdding(properties);
        }

        /// <summary>
        /// Asynchronous After event that occurs after a new item has been added to its containing object.
        /// </summary>
        /// <param name="properties"></param>
        public override void ItemAdded(SPItemEventProperties properties)
        {
            if (Helper.ListItemIsTask(properties.ListItem))
            {

            }

            base.ItemAdded(properties);
        }

        /// <summary>
        /// Synchronous Before event that occurs when an existing item is changed, for example, when the user changes data in one or more fields.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPItemEventProperties"/> object that represents properties of the event handler.</param>
        public override void ItemUpdating(SPItemEventProperties properties)
        {
            if (Helper.ListItemIsTask(properties.ListItem))
            {
                //If a task is marked as complete, change it's entry in the parent task
                if ((string)properties.AfterProperties[Helper.Task_PercentComplete] == "1")
                {
                    SPListItem parentTask = Helper.FindParentTask(properties.List, properties.ListItem.UniqueId);

                    if (parentTask != null)
                    {
                        Dictionary<Guid, bool> tasks;

                        tasks = Helper.DeserializeSubTasks((string)parentTask[Helper.WorkflowTaskReplicator_SubTasks]);

                        if (tasks.ContainsKey(properties.ListItem.UniqueId))
                        {
                            tasks[properties.ListItem.UniqueId] = true;

                            parentTask[Helper.WorkflowTaskReplicator_SubTasks] = Helper.SerializeSubTasks(tasks);

                            //Workaround for workflow task locked issue when updating
                            if ((int)parentTask[SPBuiltInFieldId.WorkflowVersion] != 1)
                                parentTask[SPBuiltInFieldId.WorkflowVersion] = 1;

                            parentTask.Update();
                        }
                        else
                            Helper.Log(String.Format("Task '{0}' not in parent task '{1}' list of sub tasks.", properties.ListItem.UniqueId, parentTask.UniqueId));
                    }
                }
            }

            base.ItemUpdating(properties);
        }

        /// <summary>
        /// Asynchronous After event that occurs after an existing item is changed, for example, when the user changes data in one or more fields.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPItemEventProperties"/> object that represents properties of the event handler.</param>
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            if (Helper.ListItemIsTask(properties.ListItem))
            {
                
            }

            base.ItemUpdated(properties);
        }

        /// <summary>
        /// Synchronous Before event that occurs before an existing item is completely deleted.
        /// </summary>
        /// <param name="properties"></param>
        public override void ItemDeleting(SPItemEventProperties properties)
        {
            if (Helper.ListItemIsTask(properties.ListItem))
            {
                
            }

            base.ItemDeleting(properties);
        }

        /// <summary>
        /// Asynchronous After event that occurs after an existing item is completely deleted.
        /// </summary>
        /// <param name="properties"></param>
        public override void ItemDeleted(SPItemEventProperties properties)
        {
            if (Helper.ListItemIsTask(properties.ListItem))
            {

            }

            base.ItemDeleted(properties);
        }
    }
}